﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OpenPath
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       private static string jsonFileName = "list.json";
        private List<PathModle> pathModles;
        private Action AddNewPath;
        private void Form1_Load(object sender, EventArgs e)
        {
            pathModles = new List<PathModle>();
            AddNewPath += ReNewList;
            ReNewList();
        }
        private void ShowInView()
        {
            
            if (pathModles!=null)
            {
                listBoxList.Items.Clear();
                pathModles.ForEach((p)=> {
                    listBoxList.Items.Add(p);
                });
            }
        
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ReNewList();
        }

        private void ReNewList()
        {
            if (File.Exists(jsonFileName))
            {
                string fileString = File.ReadAllText(jsonFileName);
                pathModles = JsonConvert.DeserializeObject<List<PathModle>>(fileString);
                ShowInView();
            }
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddNew addNew = new AddNew(AddNewPath);

            addNew.ShowDialog();
        }

        private void listBoxList_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                PathModle pathModle = (PathModle)listBoxList.SelectedItem;


                System.Diagnostics.Process.Start("explorer.exe", pathModle.Path);

            }
            catch  
            {

                 
            }
          
        }

        //新增了

    }
}
